﻿using System;
using System.Web.Http;
using ZQNB.Common.Caching;
using ZQNB.Common.Ioc;

namespace ZQNB.Web.Common.Controllers.Api
{
    public class TestController : ApiController
    {
        // demo: api/Test/GetById/1
        public string GetById(int id)
        {
            IPerSessionCacheManager perSessionCacheManager = CoreServiceProvider.LocateService<IPerSessionCacheManager>();
            var date = perSessionCacheManager.Get<string>("test");
            if (date == null)
            {
                //默认ApiController是不支持Session的！！！
                //todo...
                date = DateTime.Now.ToString();
                perSessionCacheManager.Set("test", date);
            }
            var result = "[WEBAPI] GetById from session : " + date + ", page refresh at : " + DateTime.Now;
            return result;
        }

        public string GetByName(int id)
        {
            IPerSessionCacheManager perSessionCacheManager = CoreServiceProvider.LocateService<IPerSessionCacheManager>();
            var date = perSessionCacheManager.Get<string>("test");
            if (date == null)
            {
                //默认ApiController是不支持Session的！！！
                //todo...
                date = DateTime.Now.ToString();
                perSessionCacheManager.Set("test", date);
            }
            var result = "[WEBAPI] GetByName from session : " + date + ", page refresh at : " + DateTime.Now;
            return result;
        }

        public string GetByName()
        {
            IPerSessionCacheManager perSessionCacheManager = CoreServiceProvider.LocateService<IPerSessionCacheManager>();
            var date = perSessionCacheManager.Get<string>("test");
            if (date == null)
            {
                //默认ApiController是不支持Session的！！！
                //todo...
                date = DateTime.Now.ToString();
                perSessionCacheManager.Set("test", date);
            }
            var result = "[WEBAPI] GetByName() from session : " + date + ", page refresh at : " + DateTime.Now;
            return result;
        }
    }


    ////默认ApiController是不支持Session的！！！
    ////todo...
    //[RoutePrefix("Api/Test")]
    //public class TestController :  ApiController
    //{
    //    // demo: api/Test/GetById/1
    //    [HttpGet]
    //    [Route("GetById/{id}")]
    //    public string GetById(int id)
    //    {
    //        IPerSessionCacheManager perSessionCacheManager = CoreServiceProvider.LocateService<IPerSessionCacheManager>();
    //        var date = perSessionCacheManager.Get<string>("test");
    //        if (date == null)
    //        {
    //            //默认ApiController是不支持Session的！！！
    //            //todo...
    //            date = DateTime.Now.ToString();
    //            perSessionCacheManager.Set("test", date);
    //        }
    //        var result = "[WEBAPI] from session : " + date + ", page refresh at : " + DateTime.Now;
    //        return result;
    //    }
    //}
}
